<?php
namespace app\common\model;
use think\Db;
/**
 * 菜单模型
 */
class HomeMenu extends Base {
	/**
	 * 获取前台需要验证会员是否登录的url
	 */
	public function listQuery(){
		$rs = cache('ZHT_PROTECTED_URL');
		if(!$rs){
			$list = $this->where('dataFlag',1)->order('menuType asc')->select();
			$rs = [];
			foreach($list as $key => $v){
				$rs[strtolower($v['menuUrl'])] = $v['menuType'];
				if($v['menuOtherUrl']!=''){
					$str = explode(',',$v['menuOtherUrl']);
					foreach ($str as $vkey => $vv){
						if($vv=='')continue;
						$rs[strtolower($vv)] = $v['menuType'];
					}
				}
			}
			cache('ZHT_PROTECTED_URL',$rs,31536000);
		}
		return $rs;
	}
	/**
	 * 获取会员中心菜单对应的menuId，只针对买家菜单
	 */
	public function listQuery2(){
		$rs = cache('ZHT_PROTECTED_URL2');
		if(!$rs){
			$list = $this->where(['dataFlag'=>1,'menuType'=>1])->order('menuType asc')->select();
			$rs = [];
			foreach($list as $key => $v){
				$rs[strtolower($v['menuUrl'])] = $v['menuId'];
				if($v['menuOtherUrl']!=''){
					$str = explode(',',$v['menuOtherUrl']);
					foreach ($str as $vkey => $vv){
						if($vv=='')continue;
						$rs[strtolower($vv)] = $v['menuId'];
					}
				}
			}
			cache('ZHT_PROTECTED_URL2',$rs,31536000);
		}
		return $rs;
	}
	/**
	 * 获取前台需要验证会员是否登录的url-只针对商家菜单
	 */
	public function getOperateUrl(){
		$rs = cache('ZHT_OPERATE_URL');
		if(!$rs){
			$list = $this->where(['dataFlag'=>1,'menuType'=>2])->order('menuType asc')->select();
			$rs = [];
			$arr = [];
			foreach($list as $key => $v){
				$arr['menuId'] = $v['menuId'];
				$arr['menuTitle'] = $v['menuTitle'];
				if($v['menuUrl']!='' and $v['menuUrl']!='#'){
					$rs[strtolower($v['menuUrl'])] = $arr;
				}
				if($v['menuOtherUrl']!=''){
					$str = explode(',',$v['menuOtherUrl']);
					foreach ($str as $vkey => $vv){
						if($vv=='')continue;
						$rs[strtolower($vv)] = $arr;
					}
				}
			}
			cache('ZHT_OPERATE_URL',$rs,31536000);
		}
		return $rs;
	}
	/**
	 * 获取菜单树
	 */
	public function getMenus(){
		$data = cache('ZHT_HOME_MENU');
		if(!$data){
			$rs = $this->where(['isShow'=>1,'dataFlag'=>1])
			        ->field('menuId,parentId,menuTitle,menuUrl,menuUrlParam,menuType')->order('dataSort desc,menuId asc')->select();
			$m1 = ['1'=>[],'2'=>[]];
			$tmp = [];
			
			//获取第一级
			foreach ($rs as $key => $v){
				if($v['parentId']==0){
					$m1[$v['menuType']][$v['menuId']] = ['menuId'=>$v['menuId'],'parentId'=>$v['parentId'],'menuTitle'=>$v['menuTitle'],'menuUrl'=>$v['menuUrl'],'menuUrlParam'=>$v['menuUrlParam']];
				}else{
					$tmp[$v['parentId']][] = ['menuId'=>$v['menuId'],'parentId'=>$v['parentId'],'menuTitle'=>$v['menuTitle'],'menuUrl'=>$v['menuUrl'],'menuUrlParam'=>$v['menuUrlParam']];
				}
			}
			//获取第二级
			foreach ($m1 as $key => $v){
				foreach ($v as $key1 => $v1){
				    if(isset($tmp[$v1['menuId']]))$m1[$key][$key1]['list'] = $tmp[$v1['menuId']];
				}
			}
			//获取第三级
		    foreach ($m1 as $key => $v){
		    	foreach ($v as $key1 => $v1){
			    	if(isset($v1['list'])){
				    	foreach ($v1['list'] as $key2 => $v2){
						    if(isset($tmp[$v2['menuId']]))$m1[$key][$key1]['list'][$key2]['list'] = $tmp[$v2['menuId']];
				    	}
			    	}
		    	}
			}
			cache('ZHT_HOME_MENU',$m1,31536000);
			return $m1;
		}
		return $data;
	}
}